﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Runda.CMS.Bll;
using Runda.CMS.Common.Entity;
using Telerik.WebControls;
using Runda.CMS.Common.Utility;

namespace Runda.CMS.Portal.ContractFollowUp
{
    public partial class AccessorySetting : BasePage
    {
        public int DictionaryType
        {
            get
            {
                if (Request.QueryString["Type"] != null)
                {
                    return Convert.ToInt32(Request.QueryString["Type"]);
                }
                else
                {
                    return -1;
                }
            } 
        }

        public override bool IsCheckAccessible
        {
            get
            {
                return false;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ltlTitle.Text = EnumeratorHelper.GetFieldText((Enumerator.DictionaryType)DictionaryType);
                hidType.Value = DictionaryType.ToString();
            }
        }

        protected void BindGrid(bool isneedbind)
        {
            MasterDataBll bll = new MasterDataBll();

            this.DictionaryList.DataSource = bll.GetDictionaryListFromCache((Runda.CMS.Common.Utility.Enumerator.DictionaryType)DictionaryType);

            if(isneedbind)
            DictionaryList.DataBind();            
        }

        protected void DictionaryList_ItemCommand(object source, Telerik.WebControls.GridCommandEventArgs e)
        {
            if (e.CommandSource is LinkButton)
            {
                LinkButton l = (LinkButton)e.CommandSource;

                if (l.CommandName.ToLower() == "delete")
                {
                    string id = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["ID"].ToString();

                    MasterDataBll bll = new MasterDataBll();
                    bll.LogicDelete( new SysDictionaryEntity(new Guid(id)));

                } 
            }
        }

        protected void DictionaryList_UpdateCommand(object source, Telerik.WebControls.GridCommandEventArgs e)
        {
            if (e.CommandName.ToLower() == "update")
            {
                string ID = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["ID"].ToString();
                TextBox txtName = e.Item.FindControl("txtName") as TextBox; 

                SysDictionaryEntity DictEntity = null;
                MasterDataBll Dictbll = new MasterDataBll();
                DictEntity = Dictbll.Get<SysDictionaryEntity>(new Guid(ID));
                if (DictEntity != null)
                {
                    DictEntity.Name = txtName.Text;
                    Dictbll.Update(DictEntity);
                }


                DictionaryList.MasterTableView.ClearEditItems();
                DictionaryList.EditIndexes.Clear();
                DictionaryList.MasterTableView.IsItemInserted = false;
            }
        }

      

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            BindGrid(true);
        }

        protected void DictionaryList_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
        {
            BindGrid(false);
        }

        protected void DictionaryList_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item.ItemType == GridItemType.Item || e.Item.ItemType == GridItemType.AlternatingItem)
            {
                LinkButton lnkbtnEdit = e.Item.FindControl("lnkbtnEdit") as LinkButton;

                SysDictionaryEntity entity = e.Item.DataItem as SysDictionaryEntity;
                if(entity != null)
                    lnkbtnEdit.Attributes.Add("onclick", "radopen('AccessorySettingAdd.aspx?ID="+entity.ID+"','winEditItem');return false;");
            }
        }
 
    }
}
